<?php
/*
 * 商品类
 *
 */
namespace Home\Logic;

class ShopmealsetLogic{
    
    public function __construct() {
        $this->sms = D('Shop_meal_set');
        import('Memcachectr');
        $this->m = \Think\Memcachectr::create();
    }
    
    public function setStoreId($store_id){
        $this->sms->setTableNamePre($store_id);
    }
    
    public function getItemByMealSetId($meal_set_id,$store_id,$bool=false){
        if($store_id<1){
            throw new \Exception('shop_id is null');
        }
        $this->setStoreId($store_id);
        if($bool){
            $where ='meal_set_id='.$meal_set_id;
        }else{
            $where = 'meal_set_id='.$meal_set_id.' and status != 0';
        }
        return $this->sms->where($where)->select();
    }

    public function insertMealset($data){
        if($data['store_id']<1){
            throw new \Exception('shop_id is null');
        }
        $this->m->delete('item_' .$store_id.'_'. $re['id']);
        $this->m->delete('store_items_' . $store_id );
        $this->setStoreId($data['store_id']);
    	return $this->sms->add($data);
    }
    public function updateMealset($id,$data,$store_id){
        if($store_id<1){
            throw new \Exception('shop_id is null');
        }
        $this->setStoreId($store_id);
        $where = 'id='.$id;
        if($data==0){
            $arr['status'] = $data;
        }else{
            $arr['meal_set_id'] = $data;
        }
        $this->m->delete('item_' .$store_id.'_'. $re['id']);
        $this->m->delete('store_items_' . $store_id );
        $this->m->delete('item_' . $id);
        $this->m->delete('store_items_' . $store_id . "_1");
    	return $this->sms->where($where)->save($arr);


    }

    public function getMealSetsByMember($member,$meal_set_id,$store_id){
        if($store_id<1){
            throw new \Exception('shop_id is null');
        }
        $this->setStoreId($store_id);
        $where = " meal_set_number='$member' and meal_set_id=".$meal_set_id; 
        $sql = "select * from (select * from ( select * from shop_meal_set_".substr($store_id,-1)."   where " . $where . "  order by id desc ) as a  group by a.item_id order by id desc) as b " . $wh . " order by id desc limit 3" ;
        $items = $this->sms->_read_data($this->sms->query($sql));
        return $items;
    }

    public function thingUpdateMealset($meal,$store_id){
        foreach($meal as $k=>$v){
            $status = 0;
            $re[$k] = $this->updateMealset($v['id'],$status,$store_id);
        }
        $count = count($re);
        for($i=0;$i<$count;$i++){
            if($re[$i]===false){
                return false;
            }
        }
        return true;
    }

    public function thingInsertMealset($data,$id,$item_number,$store_id,$type){
        if($type==0){
            foreach($data as $k=>$v){
                $v['meal_set_id'] = $id;
                $v['meal_set_number'] = $item_number;
                $v['status'] =1;
                $v['create_time'] = time();
                $v['store_id'] = $store_id;
                $re[$k] = $this->insertMealset($v);
            }
            
        }elseif($type==1){
            foreach($data['items'] as $k=>$v ){
                    if($v<1 || $data['num'][$k] <1){
                        continue;
                    } 
                    $data['number'] = $data['num'][$k];
                    $data['item_id'] = $v;
                    $re[$k] = $this->insertMealset($data);
            } 
        }
        $count = count($re);
        for($i=0;$i<$count;$i++){
            if($re[$i]===false ||$re[$i]==NULL){
                return false;
            }
        }
        return true;

    }



    
}